package com.mdp.im.core.service.admin;

import com.mdp.core.api.CacheHKVService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Service
public class BlackUserGroupsService {

	public static  String prefix="im_black_user_groups_";

 	public static CacheHKVService redisTemplate;

	@Autowired
	public  void setRedisTemplate(CacheHKVService redisTemplate) {
		BlackUserGroupsService.redisTemplate = redisTemplate;
	}
	
	 public static boolean isBlack(String myUserid,String blackGroupId){
		Map<String,Long> blackGroups= getBlackGroups(myUserid);
		if(blackGroups==null) {
			return false;
		}else {
			
			Long i=blackGroups.get(blackGroupId);
			if(i==null) {
				return false;
			}else {
				Date d=new Date();
				if(i.longValue()>d.getTime()) {
					return true;
				}else{
					blackGroups.remove(blackGroupId);
					return false;
				}
			} 
		}
		
	}
	
	public static void putBlack(String myUserid,String blackGroupId,Long millSec){
		if(millSec==null) {
			millSec=new Long(3600000);
		}
		Map<String,Long> blackGroups=getBlackGroups(myUserid);
		if(blackGroups==null) {
			blackGroups=new HashMap<>(); 
			Date d=new Date();
			
			blackGroups.put(blackGroupId, d.getTime() + millSec);
			putBlackGroups(myUserid,blackGroups);
		}else {
			Date d=new Date(); 
			blackGroups.put(blackGroupId, d.getTime()  + millSec);
			putBlackGroups(myUserid,blackGroups);
		}
		
	}
	 public static void removeBlack(String myUserid,String blackGroupId){
		Map<String,Long> blackGroups=getBlackGroups(myUserid);
		if(blackGroups!=null) { 
			blackGroups.remove(blackGroupId);
			putBlackGroups(myUserid,blackGroups);
		} 
		
	}
	public static Map<String,Long> getBlackGroups(String myUserid){
		return (Map<String, Long>) redisTemplate.get(prefix+myUserid,myUserid);
	}
	public static void putBlackGroups(String myUserid,Map<String,Long> blackGroups){
		redisTemplate.put(prefix+myUserid,myUserid,blackGroups);
	}

}
